﻿//-------------------------------------------------------------------------------------------------------------------------------------
// Copyright (C) 2005 greatelsoft.com All Rights Reserved
//-------------------------------------------------------------------------------------------------------------------------------------

using System;
using System.Data.Common;
using System.Data;
using System.Data.OleDb;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using Pub.Class;
using GTS.MiniBox.Entity;

namespace GTS.MiniBox.Helper {
    public partial class OriginLogInfoHelper {
        /// <summary>
        /// 批次缓存多少秒 x 5
        /// </summary>
        public static int cacheSeconds = 1440;

        public static IList<OriginLogInfo> SelectByTime(string dbkey, int groupType, int cstcode, int originID, string startTime, string endTime) {
            string cacheNameKey = "GTS.MiniBox.SelectByTimeCache_SelectByTime_{0}_{1}_{2}_{3}_{4}".FormatWith(groupType, cstcode, originID, startTime, endTime);
            return Cache2.Get<IList<OriginLogInfo>>(cacheNameKey, cacheSeconds, () => {
                DbParameter[] dbparam = new DbParameter[]{
                    Data.MakeInParam("@GroupType", (DbType)SqlDbType.Int, 4, groupType),
                    Data.MakeInParam("@Cstcode", (DbType)SqlDbType.Int, 4, cstcode),
                    Data.MakeInParam("@OriginID", (DbType)SqlDbType.Int, 4, originID),
                    Data.MakeInParam("@StartTime", (DbType)SqlDbType.VarChar, 23, startTime),
                    Data.MakeInParam("@EndTime", (DbType)SqlDbType.VarChar, 23, endTime)
                };
                return Data.Pool(dbkey).GetDataTable("report_originlog", dbparam).ToList<OriginLogInfo>();
            });
        }

        public static string GetOriginLogByProvinceCodeList(string dbkey, string provinceCodeList, string startTime, string endTime) {
            DateTime m = "".ToDateTime();
            int begin = m.Day;
            int end = m.GetLastDayOfMonth().Day;

            string cacheNameKey = "GTS.MiniBox.GetOriginLogByProvinceCodeList_{0}_{1}_{2}".FormatWith(provinceCodeList, startTime, endTime);
            IList<OriginLogInfo> list = Cache2.Get<IList<OriginLogInfo>>(cacheNameKey, cacheSeconds, () => {
                DbParameter[] dbparam = new DbParameter[]{
                    Data.MakeInParam("@ProvinceCodeList", (DbType)SqlDbType.VarChar, -1,provinceCodeList),
                    Data.MakeInParam("@StartTime", (DbType)SqlDbType.VarChar, 23, startTime),
                    Data.MakeInParam("@EndTime", (DbType)SqlDbType.VarChar, 23, endTime)
                };
                return Data.Pool(dbkey).GetDataTable("report_OriginLogByProvinceList", dbparam).ToList<OriginLogInfo>();
            });

            StringBuilder xml = new StringBuilder();
            xml.AppendFormat("<graph caption='省份接收金额' subcaption='{0}-{1}' formatNumberScale='0' decimalPrecision='0' showvalues='0' numdivlines='3' numVdivlines='0' yaxisminvalue='1000' yaxismaxvalue='1800' rotateNames='1' baseFont='宋体' baseFontSize='11' outCnvBaseFont='宋体' outCnvBaseFontSize='12'>", startTime.Replace("-", ""), endTime.Replace("-", ""));
            xml.Append("<categories>");
            list.Distinct(p => p.Day).Do(p => {
                xml.AppendFormat("<category name='{0}' />", p.Day.ToDate());
            });
            xml.Append("</categories>");
            list.Distinct(p => p.GroupID).Do(c => {
                string color = Rand.RndColor(false).ToHex();
                xml.AppendFormat("<dataset seriesName='{1}' color='{0}' anchorBorderColor='{0}' anchorBgColor='{0}'>", color, Base.GetProvince(c.GroupID.Value));
                list.Distinct(p => p.Day).Do(p => {
                    var info = list.Where(d => d.GroupID == c.GroupID && p.Day == d.Day).FirstOrDefault();
                    xml.AppendFormat("<set value='{0}' color='{1}' />", info.IsNull() || info.GroupID.IsNull() ? 0 : info.ReceiveMoney, color);
                });
                xml.Append("</dataset>");
            });
            xml.Append("</graph>");
            return xml.ToString();
        }
    }
}

